package com.spark.incoicings.conroller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spark.incoicings.service.*;
import com.spark.invoicings.entity.*;
import com.spark.invoicings.spark_enum.SparkEnum;
import com.spark.invoicings.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/demand")
@Slf4j
public class InventoryController {

    @Resource
    private InboundDetailService inboundDetailService;

    @Resource
    private WarehouseReceiptService warehouseReceiptService;

    @Resource
    private SalesOrderService salesOrderService;

    @Resource
    private OutboundOrderService outboundOrderService;

    @Resource
    private OutboundDetailService outboundDetailService;

    @Resource
    private PurchaseReturnOrderService purchaseReturnOrderService;

    @Resource
    private PurchaseReturnDetailsService purchaseReturnDetailsService;

    @Resource
    private DeliveryOrderService deliveryOrderService;

    @Resource
    private OrderDetailService orderDetailService;

    @Resource
    private OrderService orderService;

    /**
     * 查询所有为待入库的采购订单
     * @return
     */
    @GetMapping("/selectOrderStatusAll")
    public Result selectOrderStatusAll() {
        List<Order> data = orderService.selectOrderStatusAll();
        return Result.success(200, "success", data);
    }

    /**
     * 根据销售订单id 获取商品
     * @param id
     * @return
     */
    @GetMapping("/selectOrderDetailsSalesByIds")
    public Result selectOrderDetailsSalesByIds(@RequestParam Integer id) {
        List<OrderDetail> data = orderDetailService.selectOrderDetailsSalesByIds(id);
        return Result.success(200, "success", data);
    }

    /**
     * 查询所属发货单
     * @return
     */
    @GetMapping("/selectDeliveryOrdes")
    public Result selectDeliveryOrder() {
        return deliveryOrderService.selectDeliveryOrder();
    }

    /**
     * 更新采购退货明细
     * @param purchaseReturnDetails
     * @return
     */
    @PostMapping("/updatePurchaseReturnDetails")
    public Result updatePurchaseReturnDetails(@RequestBody PurchaseReturnDetails purchaseReturnDetails) {
        return purchaseReturnDetailsService.updatePurchaseReturnDetails(purchaseReturnDetails);
    }

    /**
     * 保存采购退货明细
     * @param purchaseReturnDetails
     * @return
     */
    @PostMapping("/addPurchaseReturnDetails")
    public Result addPurchaseReturnDetails(@RequestBody PurchaseReturnDetails purchaseReturnDetails) {
        return purchaseReturnDetailsService.addPurchaseReturnDetails(purchaseReturnDetails);
    }

    /**
     * 根据id获取采购退货明细
     * @param id
     * @return
     */
    @GetMapping("/selectPurchaseReturnDetailsById")
    public Result selectPurchaseReturnDetailsById(@RequestParam Integer id) {
        PurchaseReturnDetails data = purchaseReturnDetailsService.selectPurchaseReturnDetailsById(id);
        return Result.success(200, "success", data);
    }

    /**
     * 查询所有采购退货明细
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/selectPurchaseReturnDetailsAll")
    public Result selectPurchaseReturnDetailsAll(@RequestParam(defaultValue = "1")
                                               Integer pageNum,
                                               @RequestParam(defaultValue = "10")
                                               Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map = new HashMap<>();
        List<PurchaseReturnDetails> list = purchaseReturnDetailsService.selectPurchaseReturnDetailsAll(map);
        PageInfo<PurchaseReturnDetails> data = new PageInfo<>(list);
        return Result.success(200, "success", data);
    }

    /**
     * 更新退货单
     * @param purchaseReturnOrder
     * @return
     */
    @PostMapping("/updatePurchaseReturnOrder")
    public Result updatePurchaseReturnOrder(@RequestBody PurchaseReturnOrder purchaseReturnOrder) {
        return purchaseReturnOrderService.updatePurchaseReturnOrder(purchaseReturnOrder);
    }

    /**
     * 保存退货单
     * @param purchaseReturnOrder
     * @return
     */
    @PostMapping("/addPurchaseReturnOrder")
    public Result addPurchaseReturnOrder(@RequestBody PurchaseReturnOrder purchaseReturnOrder) {
        return purchaseReturnOrderService.addPurchaseReturnOrder(purchaseReturnOrder);
    }

    /**
     * 根据id获取采购退货单
     * @param id
     * @return
     */
    @GetMapping("/selectPurchaseReturnOrderById")
    public Result selectPurchaseReturnOrderById(@RequestParam Integer id) {
        PurchaseReturnOrder data = purchaseReturnOrderService.selectPurchaseReturnOrderById(id);
        return Result.success(200, "success", data);
    }

    /**
     * 查询所有退货单
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/selectPurchaseReturnOrderAll")
    public Result selectPurchaseReturnOrderAll(@RequestParam(defaultValue = "1")
                                          Integer pageNum,
                                          @RequestParam(defaultValue = "10")
                                          Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map = new HashMap<>();
        List<PurchaseReturnOrder> list = purchaseReturnOrderService.selectPurchaseReturnOrderAll(map);
        PageInfo<PurchaseReturnOrder> data = new PageInfo<>(list);
        return Result.success(200, "success", data);
    }

    /**
     * 根据id获取出库单明细
     * @param id
     * @return
     */
    @GetMapping("/selectOutboundDetailById")
    public Result selectOutboundDetailById(@RequestParam Integer id) {
        OutboundDetail data = outboundDetailService.selectOutboundDetailById(id);
        return Result.success(200, "success", data);
    }

    /**
     * 保存出库单明细
     * @param OutboundDetail
     * @return
     */
    @PostMapping("/updateOutboundDetail")
    public Result updateOutboundDetail(@RequestBody OutboundDetail OutboundDetail) {
        return outboundDetailService.updateOutboundDetail(OutboundDetail);
    }

    /**
     * 保存出库单明细
     * @param OutboundDetail
     * @return
     */
    @PostMapping("/addOutboundDetail")
    public Result addOutboundDetail(@RequestBody OutboundDetail OutboundDetail) {
        return outboundDetailService.addOutboundDetail(OutboundDetail);
    }

    /**
     * 根据id获取出库单
     * @param id
     * @return
     */
    @GetMapping("/selectOutboundOrderById")
    public Result selectOutboundOrderById(@RequestParam Integer id) {
        OutboundOrder data = outboundOrderService.selectOutboundOrderById(id);
        return Result.success(200, "success", data);
    }

    /**
     * 更新出库单
     * @param outboundOrder
     * @return
     */
    @PostMapping("/updateOutboundOrderStatusById")
    public Result updateOutboundOrderStatusById(@RequestBody OutboundOrder outboundOrder) {
        outboundOrderService.updateById(outboundOrder);
        return Result.success(200, "success", outboundOrder);
    }

    /**
     * 更新出库单
     * @param outboundOrder
     * @return
     */
    @PostMapping("/updateOutboundOrder")
    public Result updateOutboundOrder(@RequestBody OutboundOrder outboundOrder) {
        return outboundOrderService.updateOutboundOrder(outboundOrder);
    }

    /**
     * 保存出库单
     * @param outboundOrder
     * @return
     */
    @PostMapping("/addOutboundOrder")
    public Result addOutboundOrder(@RequestBody OutboundOrder outboundOrder) {
        return outboundOrderService.addOutboundOrder(outboundOrder);
    }

    /**
     * 更新入库明细
     * @param inboundDetail
     * @return
     */
    @PostMapping("/updateInboundDetail")
    private Result updateInboundDetail(@RequestBody InboundDetail inboundDetail) {
        return inboundDetailService.updateInboundDetail(inboundDetail);
    }

    /**
     * 根据id回显入库单明细数据
     * @param id
     * @return
     */
    @GetMapping("/selectInboundDetailByInboundId")
    public Result selectInboundDetailByInboundId(@RequestParam Integer id) {
        InboundDetail data = inboundDetailService.selectInboundDetailByInboundId(id);
        return Result.success(200, "success", data);
    }

    /**
     * 保存入库明细
     * @param inboundDetail
     * @return
     */
    @PostMapping("/addInboundDetail")
    private Result addInboundDetail(@RequestBody InboundDetail inboundDetail) {
        return inboundDetailService.addInboundDetail(inboundDetail);
    }

    /**
     * 保存和更新入库单明细数据加载
     * @return
     */
    @GetMapping("/selectInboundDetailSave")
    public Result selectInboundDetailSave() {
        return inboundDetailService.selectInboundDetailSave();
    }

    /**
     * 查询所有销售订单
     * @return
     */
    @GetMapping("/selectSalesOrderAll")
    public Result selectSalesOrderAll() {
        List<SalesOrder> data = salesOrderService.list();
        return Result.success(200, "success", data);
    }

    /**
     * 根据id更新入库单状态
     * @param warehouseReceipt
     * @return
     */
    @PostMapping("/updateWarehouseReceiptStatusById")
    public Result updateWarehouseReceiptStatusById(@RequestBody WarehouseReceipt warehouseReceipt) {
        warehouseReceiptService.updateById(warehouseReceipt);
        return Result.success(200, "success", warehouseReceipt);
    }

    /**
     * 根据id更新入库单
     * @param warehouseReceipt
     * @return
     */
    @PostMapping("/updateWarehouseReceiptById")
    public Result updateWarehouseReceiptById(@RequestBody WarehouseReceipt warehouseReceipt) {
        if(warehouseReceiptService.updateWarehouseReceiptById(warehouseReceipt)) return Result.success();
        return Result.error(SparkEnum.UPDATE_EXCEPTION.getCode(), SparkEnum.UPDATE_EXCEPTION.getMsg());
    }

    /**
     * 根据id获取入库单
     * @param id
     * @return
     */
    @GetMapping("/selectWarehouseReceiptById")
    public Result selectWarehouseReceiptById(@RequestParam Integer id) {
        WarehouseReceipt data = warehouseReceiptService.selectWarehouseReceiptById(id);
        return Result.success(200, "success", data);
    }

    /**
     * 添加入库单
     * @param warehouseReceipt
     * @return
     */
    @PostMapping("/addWarehouseReceipt")
     public Result addWarehouseReceipt(@RequestBody WarehouseReceipt warehouseReceipt) {
         return warehouseReceiptService.addWarehouseReceipt(warehouseReceipt);
     }

    /**
     * 查询所有出库单明细
     * @param pageNum
     * @param pageSize
     * @param outboundNumber
     * @return
     */
    @GetMapping("/selectOutboundDetailAll")
    public Result selectOutboundDetailAll(@RequestParam(defaultValue = "1")
                                         Integer pageNum,
                                         @RequestParam(defaultValue = "10")
                                         Integer pageSize,
                                         @RequestParam(required = false)
                                         String outboundNumber) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("outboundNumber", outboundNumber);
        List<OutboundDetail> list = outboundDetailService.selectOutboundDetailAll(map);
        PageInfo<OutboundDetail> data = new PageInfo<>(list);
        return Result.success(200, "success", data);
    }

    /**
     * 查询所有出库单
     * @param pageNum
     * @param pageSize
     * @param outboundNumber
     * @return
     */
    @GetMapping("/selectOutboundOrderAll")
    public Result selectOutboundOrderAll(@RequestParam(defaultValue = "1")
                                         Integer pageNum,
                                         @RequestParam(defaultValue = "10")
                                         Integer pageSize,
                                         @RequestParam(required = false)
                                         String outboundNumber) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("outboundNumber", outboundNumber);
        List<OutboundOrder> list = outboundOrderService.selectOutboundOrderAll(map);
        PageInfo<OutboundOrder> data = new PageInfo<>(list);
        return Result.success(200, "success", data);
    }


    /**
     * 查询所有入库单明细
     * @param pageNum
     * @param pageSize
     * @param number
     * @return
     */
    @GetMapping("/selectInboundDetailAll")
    public Result selectInboundDetailAll(@RequestParam(defaultValue = "1")
                                             Integer pageNum,
                                         @RequestParam(defaultValue = "10")
                                             Integer pageSize,
                                         @RequestParam(required = false)
                                             String number) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("number", number);
        List<InboundDetail> list = inboundDetailService.selectInboundDetailAll(map);
        PageInfo<InboundDetail> data = new PageInfo<>(list);
        return Result.success(200, "success", data);
    }

    /**
     * 查询所有入库单
     * @param pageNum
     * @param pageSize
     * @param wName
     * @return
     */
    @GetMapping("/selectWarehouseReceiptAll")
    public Result selectWarehouseReceiptAll(@RequestParam(defaultValue = "1")
                                      Integer pageNum,
                                      @RequestParam(defaultValue = "10")
                                      Integer pageSize,
                                      @RequestParam(required = false)
                                      String wName) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("wName", wName);
        List<WarehouseReceipt> list = warehouseReceiptService.selectWarehouseReceiptAll(map);
        PageInfo<WarehouseReceipt> data = new PageInfo<>(list);
        return Result.success(200, "success", data);
    }
}
